package com.qhx.rtkcloud.ntrip;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.management.Query;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

import static com.qhx.rtkcloud.ntrip.moveClient.executorService;

//@Configuration
public class moveTcpClient {


    @Value("#{${rtk.moveTcpPorts}}")
    public Map<Integer,Integer> moveTcpPorts;

    public static List<Socket> socketList = new ArrayList<>();
    public static Map<String, Socket> clientMap = new HashMap<>();



    @Bean
    public void createSocketConnect(){
        for (Map.Entry<Integer, Integer> mapEntry : moveTcpPorts.entrySet()) {
            try {
                ServerSocket serverCloudSocket = new ServerSocket(mapEntry.getKey());
                ServerSocket serverLocalSocket = new ServerSocket(mapEntry.getValue());
                new MoveTcpClientQueue(serverCloudSocket,serverLocalSocket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    /*@Bean
    public void clientTcpClient() {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(moveTcpPort);
                    while (true) {
                        Socket socket = serverSocket.accept();
                        socketMessage(socket);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }*/


    public void socketMessage(Socket socket) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                byte[] buffer = new byte[10240];
                InputStream serverSocketInputStream = null;
                try {
                    serverSocketInputStream = socket.getInputStream();
                    OutputStream outputStream2 = socket.getOutputStream();
                    while (true) {
                        int bytesRead = 0;
                        bytesRead = serverSocketInputStream.read(buffer);
                        if (bytesRead > 0) {
                            String message = new String(buffer, 0, bytesRead, "UTF-8");
                            if (message.equals("model:1") || message.equals("model:0")) {
                                if (socketList.size() > 0) {
                                    for (Socket socket1 : socketList) {
                                        OutputStream outputStream = socket1.getOutputStream();
                                        outputStream.write(message.getBytes());
                                        outputStream.flush();
                                    }
                                }
                            } else if (message.equals("queryNavModel")) {
                                //移动站连接
                                if (!socketList.contains(socket)) {
                                    socketList.add(socket);
                                }
                                //从本地服务器获取基站的状态
                                Socket socket1 = clientMap.get("local");
                                if (socket1 != null) {
                                    OutputStream outputStream1 = socket1.getOutputStream();
                                    outputStream1.write("MoveStatus".getBytes());
                                    outputStream1.flush();
                                    InputStream inputStream = socket1.getInputStream();
                                    int read = inputStream.read(buffer);
                                    if (read > 0) {
                                        String mess = new String(buffer, 0, read, "UTF-8");
                                        outputStream2.write(mess.getBytes());
                                        outputStream2.flush();
                                    }
                                }
                            } else if (message.equals("localClient")) {
                                if (!clientMap.containsKey("local")) {
                                    clientMap.put("local", socket);
                                } else {
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            buffer = new byte[10240];
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
